package com.fernfx.xingtan.my.presenter;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.MyDiamondsContract;
import com.fernfx.xingtan.my.entity.DiamondsHistroyInfoEntity;
import com.fernfx.xingtan.my.entity.MyDiamondsEntity;
import com.fernfx.xingtan.my.model.MyDiamondsModel;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/16.
 */

public class MyDiamondsPresenter<P extends MyDiamondsContract.View> implements MyDiamondsContract.Presenter{
    private P P;
    private MyDiamondsContract.Model model;

    @Override
    public void init(BaseView view) {
        P = (P)view;
        this.model = new MyDiamondsModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        model.request(requestArgsMap,new BaseIRequestCallback(){

            @Override
            public void onSuccessful(NetworkOKResult result) {
                //关闭加载框
                P.getActivity().hideLoading();
                //显示钻石交易记录
                String message = result.getMessage();
                DiamondsHistroyInfoEntity diamondsHistroyInfoEntity = FastJsonUtil.fromBean(message,DiamondsHistroyInfoEntity.class);
                if (OtherUtil.checkRequestStatus(diamondsHistroyInfoEntity)) {
                    P.showDiamondHistoryInfo(diamondsHistroyInfoEntity);
                }else {
                    ToastUtil.showCentertoast(diamondsHistroyInfoEntity.getMsg());
                }
            }
        });
    }

    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }

    @Override
    public void getDiamondTotalAmount(Map<String, Object> requestArgsMap) {
        model.getDiamondTotalAmount(requestArgsMap,new BaseIRequestCallback(){

            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                //convert json eneity
                MyDiamondsEntity myDiamondsEntity = FastJsonUtil.fromBean(message, MyDiamondsEntity.class);
                if (OtherUtil.checkRequestStatus(myDiamondsEntity)) {
                    P.showDiamondsInfo(myDiamondsEntity);
                }else {
                    ToastUtil.showCentertoast(myDiamondsEntity.getMsg());
                }
            }
        });
    }

   public static class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int leftRight;
        private int topBottom;

        //leftRight为横向间的距离 topBottom为纵向间距离
        public SpaceItemDecoration(int leftRight, int topBottom) {
            this.leftRight = leftRight;
            this.topBottom = topBottom;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            //竖直方向的
            if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
                //最后一项需要 bottom
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.bottom = topBottom;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.right = leftRight;
            } else {
                //最后一项需要right
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.right = leftRight;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.bottom = topBottom;
            }
        }
    }
}
